/** ************************* M E D I A W I K I ******************** */

function isMediaWikiPage(){
	if(home == "wpTextbox1") return true;
	else return false;
}

var wikiContent = "";
function cleanWikiContent() {
	wikiContent = currentCC.content;
    if (wikiContent == null)
        return;
        
    removeWikiMarkup("{{", "}}");

    removeWikiTags("<ref", "</ref>");
    removeWikiTags("<gallery", "</gallery>");

    removeWikiTables();

    removeWikiMarkup("<", ">");

    wikiContent = removeWikiLinks("[[File:", wikiContent);
    wikiContent = removeWikiLinks("[[file:", wikiContent);
    wikiContent = removeWikiLinks("[[Media:", wikiContent);
    wikiContent = removeWikiLinks("[[media:", wikiContent);
    wikiContent = removeWikiLinks("[[Special:", wikiContent);
    wikiContent = removeWikiLinks("[[special:", wikiContent);
    wikiContent = removeWikiLinks("[[Category:", wikiContent);
    wikiContent = removeWikiLinks("[[category:", wikiContent);
    cleanWikiLinks();
    
    removeWikiMarkup("[http://", "]");
    removeWikiMarkup("http://", " ");
    removeWikiMarkup("[mailto:", "]");

    wikiContent = wikiContent.replaceAll("#REDIRECT", "");
    wikiContent = wikiContent.replaceAll("=", "");
    wikiContent = wikiContent.replaceAll("'", "");
    wikiContent = wikiContent.replaceAll("\"", "");
    wikiContent = wikiContent.replaceAll("----", "");
    wikiContent = wikiContent.replaceAll("*", "");
    wikiContent = wikiContent.replaceAll("#", "");
    wikiContent = wikiContent.replaceAll(";", "");
    wikiContent = wikiContent.replaceAll(":", " ");
    wikiContent = wikiContent.replaceAll("|", " ");
    wikiContent = wikiContent.replaceAll("{", "");
    wikiContent = wikiContent.replaceAll("}", "");
    wikiContent = wikiContent.replaceAll("[", "");
    wikiContent = wikiContent.replaceAll("]", "");
    wikiContent = wikiContent.replaceAll("\\", " ");
    wikiContent = wikiContent.replaceAll("/", "");
    wikiContent = wikiContent.replaceAll("(", "");
    wikiContent = wikiContent.replaceAll(")", "");
    wikiContent = wikiContent.replaceAll(",", " ");
    wikiContent = wikiContent.replaceAll("-", " ");
    wikiContent = wikiContent.replaceAll("+", " ");

    wikiContent = wikiContent.replace(/(\n|\t|\v|\f|\0|EOL)/g, " ");
    wikiContent = removeMulEmptySpace(wikiContent);
    currentCC.content = wikiContent;
    // alert(wikiContent);
} 

// Removes substring between "from" and "to" strings
function removeWikiMarkup(from, to) {
    var strText = wikiContent;
    var start = strText.indexOf(from);
    var finish = strText.indexOf(to);

    while (start != -1 && finish != -1 && start < finish) {
        if (strText.indexOf(from, start + from.length) != -1 && strText.indexOf(from, start + from.length) < finish){
            start = strText.indexOf(from, start + from.length);
        }
        else {
            strText = strText.replace(strText.substring(start, finish + to.length), "");
            start = strText.indexOf(from);
            finish = strText.indexOf(to);
        }
    }
    wikiContent = strText;
}

// Removes tags and everything between star end end tag
function removeWikiTags(startTag, endTag) {
    var strText = wikiContent;
    var reg = new RegExp(startTag);

    var startTagIndex = strText.indexOf(startTag);
    var endTagIndex = strText.indexOf(endTag, startTagIndex);
    var endStartTagIndex = strText.indexOf(">", startTagIndex + startTag.length);

    while (startTagIndex != -1 && (endTagIndex != -1 || endStartTagIndex != -1)) {
        if (strText[endStartTagIndex - 1] == "/") {
            strText = strText.replace(strText.substring(startTagIndex, endStartTagIndex + 1), "");
            startTagIndex = strText.indexOf(startTag);
            endTagIndex = strText.indexOf(endTag, startTagIndex);
            endStartTagIndex = strText.indexOf(">", startTagIndex + startTag.length);
        }
        else if (strText.substring(startTagIndex + startTag.length, endTagIndex).search(reg) != -1) {
            startTagIndex = strText.indexOf(startTag, startTagIndex + startTag.length);
        }
        else {
            strText = strText.replace(strText.substring(startTagIndex, endTagIndex + endTag.length), "");
            startTagIndex = strText.indexOf(startTag);
            endTagIndex = strText.indexOf(endTag, startTagIndex);
            endStartTagIndex = strText.indexOf(">", startTagIndex + startTag.length);
        }
    }
    wikiContent = strText;
}

// Removes wiki links to images, files, ...
function removeWikiLinks(text, content) {
    var strText = content;
    var fixedStart = strText.indexOf(text);
    var start = strText.indexOf(text);
    var finish = strText.indexOf("]]", start);

    while (start != -1 && finish != -1 && start < finish) {
        if (strText.substring(start + 2, finish).search(/\[\[/) != -1) {
            start = strText.indexOf("[[", start + 2);
            finish = strText.indexOf("]]", finish + 2);
        }
        else {
            strText = strText.replace(strText.substring(fixedStart, finish + 2), "");
            fixedStart = strText.indexOf(text);
            start = strText.indexOf(text);
            finish = strText.indexOf("]]", start);
        }
    }
    return strText;
}

// Cleans internal wiki links ie: [[Main Page]]
function cleanWikiLinks() {
    var strText = wikiContent;
    var fixedStart = strText.indexOf("[[");
    var start = strText.indexOf("[[");
    var finish = strText.indexOf("]]");

    var justOne = true;

    while (start != -1 && finish != -1 && start < finish) {
        if (strText.substring(start + 2, finish).search(/\[\[/) != -1) {
            justOne = false;
            start = strText.indexOf("[[", start + 2);
            finish = strText.indexOf("]]", finish + 2);
        }
        else if (strText.substring(start, finish).search(/\|/) != -1 && justOne) {
            var subStr = strText.substring(start, finish + 2);
            strText = strText.replace(subStr, subStr.substring(subStr.indexOf("|") + 1, subStr.indexOf("]]")));
            fixedStart = strText.indexOf("[[");
            start = strText.indexOf("[[");
            finish = strText.indexOf("]]");
        }
        else if (strText.substring(start, finish).search(/(#|:)/) != -1) {
            strText = strText.replace(strText.substring(fixedStart, finish + 2), "");
            fixedStart = strText.indexOf("[[");
            start = strText.indexOf("[[");
            finish = strText.indexOf("]]");
            justOne = true;
        }
        else if (justOne) {
            strText = strText.replace(strText.substring(start, finish + 2), strText.substring(start + 2, finish));
            fixedStart = strText.indexOf("[[");
            start = strText.indexOf("[[");
            finish = strText.indexOf("]]");
        }
        else {
            strText = strText.replace(strText.substring(fixedStart, finish + 2), "");
            fixedStart = strText.indexOf("[[");
            start = strText.indexOf("[[");
            finish = strText.indexOf("]]");
            justOne = true;
        }
    }
    wikiContent = strText;
}


// Removes murkup for wiki tables
function removeWikiTables() {
    var strText = wikiContent;
    var fixedStart = strText.indexOf("{|");
    var start = strText.indexOf("{|");
    var finish = strText.indexOf("|}");

    while (start != -1 && finish != -1 && start < finish) {
        if (strText.substring(start + 2, finish).search(/\{\|/) != -1) {
            if (strText[strText.indexOf("\n", start) + 1] == "|" || strText[strText.indexOf("\n", start) + 1] == "!")
                finish = strText.indexOf("|}", finish + 2);
            else
                fixedStart = strText.indexOf("{|", start + 2);
                
            start = strText.indexOf("{|", start + 2);
        }
        else {
            var subStr = strText.substring(fixedStart, finish + 3);
            subStr = removeLine(subStr, "{|");
            subStr = removeLine(subStr, "|-");
            subStr = removeLine(subStr, "|+");
            subStr = removeLine(subStr, "|}");
            subStr = removeStyleAttributes(subStr);
            
            strText = strText.replace(strText.substring(fixedStart, finish + 3), subStr);
            fixedStart = strText.indexOf("{|");
            start = strText.indexOf("{|");
            finish = strText.indexOf("|}");
        }
    }
    wikiContent = strText;
}

// Removes the line that begins with kvaziTag
function removeLine(subStr, kvaziTag) {
    var start = subStr.indexOf(kvaziTag);
    var finish = subStr.indexOf("\n", start);

    while (start != -1 && finish != -1 && start < finish) {
        subStr = subStr.replace(subStr.substring(start, finish), "");
        start = subStr.indexOf(kvaziTag);
        finish = subStr.indexOf("\n", start);
    }
    return subStr;
}

// Removes all style for wiki tables
function removeStyleAttributes(subStr) {
    var strSplit = subStr.split("\n");
    for (var i = 0; i < strSplit.length; i++) {
        if (strSplit[i][0] == "!" && strSplit[i].search(/\|/) != -1) {
            subStr = subStr.replace(strSplit[i].substring(0, strSplit[i].indexOf("|") + 1), "");
        }
        if (strSplit[i][0] == "|" && strSplit[i].search(/\|\|/) != -1) {
            cells = strSplit[i].split("||");
            for (var j = 0; j < cells.length; j++) {
                if (cells[j].search(/\|/) != -1) {
                    subStr = subStr.replace(cells[j].substring(0, cells[j].indexOf("|") + 1), "");
                }
            }
        }
    }
    subStr = subStr.replaceAll("!", "");
    return subStr;
}


// Replaces two consecutive appearances of spaces with one space
function removeMulEmptySpace(text) {
    var strText = text;
    var start = strText.indexOf("  ");

    while (start != -1) {
        strText = strText.replace(/\s\s/g, " ");
        start = strText.indexOf("  ");
    }
    return strText;
}

// Creates trim function for string objects
String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
};


// Replaces all instances of the given substring.
// strTarget - the substring you want to replace
// strSubString - the string you want to replace in.
String.prototype.replaceAll = function(strTarget, strSubString) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);

    while (intIndexOfMatch != -1) {
        strText = strText.replace(strTarget, strSubString);
        intIndexOfMatch = strText.indexOf(strTarget);
    }
    return (strText);
};
/** ********************************************************************** */
